#!/bin/bash

# Test script to verify timing information in API responses

set -e

SERVICE_URL="http://192.168.10.11:8080"
TEST_IMAGE="/tmp/test_image.jpg"

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║         Testing Timing Information in API Responses            ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

# Check if service is healthy
echo "Checking service health..."
health_response=$(curl -s "$SERVICE_URL/health")
if echo "$health_response" | jq . > /dev/null 2>&1; then
    echo "✓ Service is healthy"
else
    echo "✗ Service is not responding"
    exit 1
fi
echo ""

# Test 1: Upload and Infer with timing
echo "Test 1: Upload and Infer Endpoint"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

response=$(curl -s -F "image=@$TEST_IMAGE" "$SERVICE_URL/api/v1/infer/upload")

echo "Full Response:"
echo "$response" | jq .

echo ""
echo "Timing Information:"
echo "$response" | jq '.timing'

echo ""
echo "Timing Breakdown:"
echo "  Validation Time:    $(echo "$response" | jq '.timing.validation_time_ms') ms"
echo "  Inference Time:     $(echo "$response" | jq '.timing.inference_time_ms') ms"
echo "  Output Parse Time:  $(echo "$response" | jq '.timing.output_parse_time_ms') ms"
echo "  Total Time:         $(echo "$response" | jq '.timing.total_time_ms') ms"
echo "  Process Time:       $(echo "$response" | jq '.process_time_ms') ms"

echo ""
echo "✓ Test completed successfully"
echo ""

# Test 2: Verify timing values are reasonable
echo "Test 2: Verify Timing Values"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

validation_time=$(echo "$response" | jq '.timing.validation_time_ms')
inference_time=$(echo "$response" | jq '.timing.inference_time_ms')
parse_time=$(echo "$response" | jq '.timing.output_parse_time_ms')
total_time=$(echo "$response" | jq '.timing.total_time_ms')

echo "Validation Time: $validation_time ms"
if (( $(echo "$validation_time > 0" | bc -l) )); then
    echo "  ✓ Validation time is positive"
else
    echo "  ✗ Validation time is zero or negative"
fi

echo "Inference Time: $inference_time ms"
if (( $(echo "$inference_time > 0" | bc -l) )); then
    echo "  ✓ Inference time is positive"
else
    echo "  ✗ Inference time is zero or negative"
fi

echo "Parse Time: $parse_time ms"
if (( $(echo "$parse_time > 0" | bc -l) )); then
    echo "  ✓ Parse time is positive"
else
    echo "  ✗ Parse time is zero or negative"
fi

echo "Total Time: $total_time ms"
if (( $(echo "$total_time > 0" | bc -l) )); then
    echo "  ✓ Total time is positive"
else
    echo "  ✗ Total time is zero or negative"
fi

echo ""
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║                    Test Summary                                ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
echo "✓ Timing information is being returned in API responses"
echo "✓ All timing values are positive and reasonable"
echo ""

